package com.lxmes.business.service.impl;

import java.util.List;

import com.lxmes.business.domain.CcChukuInfo;
import com.lxmes.business.mapper.CcChukuInfoMapper;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.business.mapper.CcChukuInfoPlanMapper;
import com.lxmes.business.domain.CcChukuInfoPlan;
import com.lxmes.business.service.ICcChukuInfoPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库计划Service业务层处理
 *
 * @author lx
 * @date 2023-06-28
 */
@Service
public class CcChukuInfoPlanServiceImpl implements ICcChukuInfoPlanService
{
    @Autowired
    private CcChukuInfoPlanMapper ccChukuInfoPlanMapper;

    @Autowired
    private CcChukuInfoMapper ccChukuInfoMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询出库计划
     *
     * @param id 出库计划主键
     * @return 出库计划
     */
    @Override
    public CcChukuInfoPlan selectCcChukuInfoPlanById(Long id)
    {
        return ccChukuInfoPlanMapper.selectCcChukuInfoPlanById(id);
    }

    /**
     * 查询出库计划列表
     *
     * @param ccChukuInfoPlan 出库计划
     * @return 出库计划
     */
    @Override
    public List<CcChukuInfoPlan> selectCcChukuInfoPlanList(CcChukuInfoPlan ccChukuInfoPlan)
    {
        return ccChukuInfoPlanMapper.selectCcChukuInfoPlanList(ccChukuInfoPlan);
    }

    /**
     * 新增出库计划
     *
     * @param ccChukuInfoPlan 出库计划
     * @return 结果
     */
    @Override
    public int insertCcChukuInfoPlan(CcChukuInfoPlan ccChukuInfoPlan)
    {
        ccChukuInfoPlan.setCreateTime(DateUtils.getNowDate());
        ccChukuInfoPlan.setDeptId(SecurityUtils.getDeptId());
        return ccChukuInfoPlanMapper.insertCcChukuInfoPlan(ccChukuInfoPlan);
    }

    /**
     * 修改出库计划
     *
     * @param ccChukuInfoPlan 出库计划
     * @return 结果
     */
    @Override
    public int updateCcChukuInfoPlan(CcChukuInfoPlan ccChukuInfoPlan)
    {
        ccChukuInfoPlan.setUpdateTime(DateUtils.getNowDate());
        return ccChukuInfoPlanMapper.updateCcChukuInfoPlan(ccChukuInfoPlan);
    }

    /**
     * 批量删除出库计划
     *
     * @param ids 需要删除的出库计划主键
     * @return 结果
     */
    @Override
    public int deleteCcChukuInfoPlanByIds(Long[] ids)
    {
        return ccChukuInfoPlanMapper.deleteCcChukuInfoPlanByIds(ids);
    }

    /**
     * 删除出库计划信息
     *
     * @param id 出库计划主键
     * @return 结果
     */
    @Override
    public int deleteCcChukuInfoPlanById(Long id)
    {
        return ccChukuInfoPlanMapper.deleteCcChukuInfoPlanById(id);
    }
    @Override
    public String imporIteminfo(List<CcChukuInfoPlan> ccChukuInfoPlanList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ccChukuInfoPlanList) || ccChukuInfoPlanList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CcChukuInfoPlan ccChukuInfoPlan : ccChukuInfoPlanList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ccChukuInfoPlan);
                ccChukuInfoPlan.setCreateBy(operName);
                ccChukuInfoPlan.setUpdateBy(operName);
                this.insertCcChukuInfoPlan(ccChukuInfoPlan);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional
    public int scChukuDan(Long[] ids) {
        for(long id : ids){
            CcChukuInfoPlan ccChukuInfoPlan = ccChukuInfoPlanMapper.selectCcChukuInfoPlanById(id);
            if("1".equals(ccChukuInfoPlan.getState())){
                throw new RuntimeException("选择项中存在已生成出库单项，请重新选择！");
            }

            CcChukuInfo ccChukuInfo = new CcChukuInfo();
            BeanUtils.copyProperties(ccChukuInfoPlan,ccChukuInfo);
            ccChukuInfoMapper.insertCcChukuInfo(ccChukuInfo);
            ccChukuInfoPlan.setState("1");
            ccChukuInfoPlanMapper.updateCcChukuInfoPlan(ccChukuInfoPlan);

        }
        return 1;
    }
}
